namespace ne {
    export class EventManager {
        private static _receivedEventsHanlder: Function;
        /** save all mod messages */
        private static _events_mod: Map<{ new(): ModuleEvent }, EventVo[]> = new Map();
        /** save all str messages */
        private static _events_str: Map<string, EventVo[]> = new Map();
        /**
         * @private
         */
        static get events() {
            return this._events_mod;
        }

        /**
         * 注册事件，初始化的时候所有的事件都被注册了
         * @param $event 模块事件
         *
         */
        static registerEvent(eventVo: EventVo, receivedEventsHanlder: Function = null): void {
            if (eventVo === null) return;
            this._receivedEventsHanlder = receivedEventsHanlder;
            let eventClass = eventVo.eventClass;
            if (eventClass) {
                if (this._events_mod.get(eventClass) === undefined) this._events_mod.set(eventClass, []);
                //判断是否存在代理
                let key = eventVo.eventClass;
                let events = this._events_mod.get(eventVo.eventClass);
                let find: boolean;
                for (let i = 0, l = events.length; i < l; i++) {
                    if (events[i].proxy === eventVo.proxy) {
                        find = true;
                        break;
                    }
                }
                if (!find)
                    events.push(eventVo);
            }
        }

        /**
         * 注册事件，初始化的时候所有的事件都被注册了
         * @param $event 模块事件
         *
         */
        static registerStringEvent(eventVo: EventVo, receivedEventsHanlder: Function = null): void {
            if (eventVo === null) return;
            this._receivedEventsHanlder = receivedEventsHanlder;
            let eventString = eventVo.eventString
            if (eventString) {
                if (this._events_str.get(eventString) === undefined) this._events_str.set(eventString, []);
                //判断是否存在代理
                let events = this._events_str.get(eventString);
                let find: boolean;
                for (let i = 0, l = events.length; i < l; i++) {
                    if (events[i].proxy === eventVo.proxy) {
                        find = true;
                        break;
                    }
                }
                if (!find)
                    events.push(eventVo);
            }
        }

        /**
         * 某个代理发送事件
         * @param $proxy 事件代理
         * @param event 事件
         *
         */
        static dispatchModuleEvent(event: ModuleEvent): void {
            if (!event) return;
            let events = this._events_mod.get(event.getClass());
            if (events) {
                for (let i = 0, l = events.length; i < l; i++) {
                    let eventVo = events[i];
                    eventVo.proxy.receivedEvents(event);
                    if (this._receivedEventsHanlder) {
                        this._receivedEventsHanlder(eventVo, event);
                    }
                }
            }
        }
        

        /**
         * 某个代理发送事件
         * @param $proxy 事件代理
         * @param event 事件
         *
         */
        static dispatchStringEvent(event: string, eventdata?: any): void {
            if (!event) return;
            let events = this._events_str.get(event);
            if (events) {
                for (let i = 0, l = events.length; i < l; i++) {
                    let eventVo = events[i];
                    eventVo.proxy.receivedStringEvents(event, eventdata);
                    if (this._receivedEventsHanlder) {
                        this._receivedEventsHanlder(eventVo, event);
                    }
                }
            }
        }
    }
}